package wangjg.coco.views;

import java.io.IOException;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import wangjg.coco.module.Source2ModelProcessor;

/**
 * 
 * @author wangjg
 *
 */
public class Source2ModelDialog extends Dialog {
	private static final String PREFERENCE_STORE_PATH = Source2ModelDialog.class.getName();
	
	private static final String S2M_PATH = "s2m_path";
	private static final String S2M_SAVE_FILED = "s2m_save_filed";
	private static final String S2M_SAVE_METHOD = "s2m_save_method";
	private static final String S2M_EXCLUDE = "s2m_exclude";
	private static final String S2M_FILTER_TEXT = "s2m_filter_text";

	private Label	outputLabel = null;
	private Text	outputDirText = null;
	private Button	outputDirChoseButton = null;

	private Group	filterGroup = null;
	private Button	filterFieldCheckBox = null;
	private Button	filterMethodCheckBox = null;
	
	private Button	excludeCheckBox = null;
	
	private Text	filterText = null;
	
	private List<IResource> resourceList;

	public Source2ModelDialog(IShellProvider parentShell) {
		super(parentShell);
	}

	public Source2ModelDialog(Shell parentShell) {
		super(parentShell);
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		this.setLayout(composite);
		
		outputLabel = new Label(composite, SWT.NONE);
		outputLabel.setText("输出文件夹");
		outputDirText = new Text(composite, SWT.BORDER);
		setGridData(outputDirText, 1);
		outputDirChoseButton = new Button(composite, SWT.NONE);
		outputDirChoseButton.setText("选择..");
		outputDirChoseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				selectFilePath("out");
			}
		});

		filterGroup = new Group(composite, SWT.NONE);
		this.setLayout(filterGroup);
		
		filterGroup.setText("过滤");
		setGridData(filterGroup, 3);
		
		filterFieldCheckBox = new Button(filterGroup, SWT.CHECK);
		filterFieldCheckBox.setText("字段");
		filterMethodCheckBox = new Button(filterGroup, SWT.CHECK);
		filterMethodCheckBox.setText("方法");
		new Label(filterGroup, SWT.NONE);
		
		//排除
		excludeCheckBox = new Button(filterGroup, SWT.CHECK);
		excludeCheckBox.setText("排除以下成员");
		excludeCheckBox.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				filterText.setEnabled(excludeCheckBox.getSelection());
			}
		});
		
		new Label(filterGroup, SWT.NONE);

		Composite textPanel = new Composite(filterGroup, SWT.NONE);
		setGridData(textPanel, 3);
		filterText = new Text(textPanel, SWT.BORDER|SWT.WRAP|SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL);
		filterText.setEnabled(false);
		filterText.setSize(220, 200);

		this.loadSetting();
		return composite;
	}

	/*
	 * 文件夹（目录）选择对话框
	 */
	private void selectFilePath(String type) {
		String dir = selectDialog(outputDirText.getText());
		if(dir!=null){
			outputDirText.setText(dir);
		}
	}
	
	private void setGridData(Control control, Integer rowSpan){
		this.setGridData(control, rowSpan, null);
	}
	
	private void setGridData(Control control, Integer rowSpan, Integer colSpan){
		GridData gd = new GridData();
		gd.horizontalSpan = rowSpan;
		if(colSpan!=null){
			gd.verticalSpan = colSpan;
		}
		gd.horizontalAlignment = SWT.FILL;
		gd.grabExcessHorizontalSpace = true;//?
		control.setLayoutData(gd);
	}
	
	private void setLayout(Composite composite){
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.makeColumnsEqualWidth=false;//?
		composite.setLayout(gridLayout);
	}

	
	private String selectDialog(String path){
		DirectoryDialog folderdlg = new DirectoryDialog(this.getShell());
		folderdlg.setText("选择文件");
		folderdlg.setFilterPath(path);
		String selectedDir = folderdlg.open();
		return selectedDir;
	}

	protected void okPressed(){
		this.processModel();
		this.saveSetting();
		super.okPressed();
	}
	
	private void processModel(){
		String targetDir = this.outputDirText.getText();
		if(targetDir!=null && targetDir.length()>0){
			Source2ModelProcessor mp = new Source2ModelProcessor();
			mp.setResourceList(resourceList);
			mp.setIncludeField(this.filterFieldCheckBox.getSelection());
			mp.setIncludeMethod(this.filterMethodCheckBox.getSelection());
			if(this.excludeCheckBox.getSelection()){
				mp.setFilter_exp(this.filterText.getText());
			}
			mp.saveToModel(targetDir);
		}
	}
	
	private void saveSetting(){
		PreferenceStore preferenceStore = new PreferenceStore(PREFERENCE_STORE_PATH);
		preferenceStore.setValue(S2M_PATH, this.outputDirText.getText());
		preferenceStore.setValue(S2M_SAVE_FILED, this.filterFieldCheckBox.getSelection());
		preferenceStore.setValue(S2M_SAVE_METHOD, this.filterMethodCheckBox.getSelection());
		preferenceStore.setValue(S2M_EXCLUDE, this.excludeCheckBox.getSelection());
		preferenceStore.setValue(S2M_FILTER_TEXT, this.filterText.getText());
		try {
			preferenceStore.save();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void loadSetting(){
		PreferenceStore preferenceStore = new PreferenceStore(PREFERENCE_STORE_PATH);
		try {
			preferenceStore.load();
		} catch (IOException e) {
			e.printStackTrace();
			return ;
		}
		this.outputDirText.setText(preferenceStore.getString(S2M_PATH));
		this.filterFieldCheckBox.setSelection(preferenceStore.getBoolean(S2M_SAVE_FILED));
		this.filterMethodCheckBox.setSelection(preferenceStore.getBoolean(S2M_SAVE_METHOD));
		this.excludeCheckBox.setSelection(preferenceStore.getBoolean(S2M_EXCLUDE));
		this.filterText.setEnabled(this.excludeCheckBox.getSelection());
		this.filterText.setText(preferenceStore.getString(S2M_FILTER_TEXT));
	}
	
	public void setResourceList(List<IResource> pathList) {
		this.resourceList = pathList;
	}
	
	protected int getShellStyle(){
        return super.getShellStyle();//|SWT.RESIZE|SWT.MAX;
    }
}